<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>文章列表</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css" />
    <link rel="stylesheet" href="css/reset.css" />
    <link rel="stylesheet" href="css/iconfont.css" />
    <link rel="stylesheet" href="css/main.css" />
    <script src="./libs/jquery-1.12.4.min.js"></script>
  </head>

  <body>
    <div class="container-fluid">
      <div class="common_title">
        用户信息修改
      </div>
      <div class="container-fluid common_con">
        <form class="form-horizontal article_form" id="form">
          <div class="form-group">
            <label for="inputEmail1" class="col-sm-2 control-label"
              >用户名称：</label
            >
            <div class="col-sm-4">
              <input
                type="text"
                name="username"
                class="form-control username"
                id="inputEmail1"
                value="李思思"
              />
            </div>
          </div>
          <div class="form-group">
            <label for="inputEmail2" class="col-sm-2 control-label"
              >昵称：</label
            >
            <div class="col-sm-4">
              <input
                type="text"
                name="nickname"
                class="form-control nickname"
                id="inputEmail2"
                value="哈哈思"
              />
            </div>
          </div>
          <div class="form-group">
            <label for="inputEmail3" class="col-sm-2 control-label"
              >邮箱：</label
            >
            <div class="col-sm-4">
              <input
                type="text"
                name="email"
                class="form-control email"
                id="inputEmail3"
                value="520@163.com"
              />
            </div>
          </div>

          <div class="form-group">
            <label for="inputPassword3" class="col-sm-2 control-label"
              >用户图标：</label
            >
            <div class="col-sm-10">
              <img src="images/2.jpg" alt="" class="user_pic" />
              <input
                style="display: block"
                name="userPic"
                type="file"
                id="exampleInputFile"
              />
              <!-- 给label标签的for属性赋值,值是input标签的id,那这样的话点击label就相当于点击了input -->
              <br />
              <label for="exampleInputFile">
                <img
                  src="./images/uploads_icon.jpg"
                  style="height: 50px;"
                  alt=""
                />
              </label>
            </div>
          </div>

          <div class="form-group">
            <label for="inputEmail4" class="col-sm-2 control-label"
              >密码：</label
            >
            <div class="col-sm-4">
              <input
                type="password"
                name="password"
                class="form-control password"
                id="inputEmail4"
                value="12345678"
              />
            </div>
          </div>
          <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
              <button type="submit" class="btn btn-success btn-edit">
                修改
              </button>
            </div>
          </div>
        </form>
      </div>
    </div>

    <!-- 5.4 引入模态框结构 -->
    <div class="modal fade" tabindex="-1" role="dialog" id="myModal">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button
              type="button"
              class="close"
              data-dismiss="modal"
              aria-label="Close"
            >
              <span aria-hidden="true">&times;</span>
            </button>
            <h4 class="modal-title">温馨提示</h4>
          </div>
          <div class="modal-body">
            <p>One fine body&hellip;</p>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">
              确认
            </button>
          </div>
        </div>
        <!-- /.modal-content -->
      </div>
      <!-- /.modal-dialog -->
    </div>
    <!-- /.modal -->
  </body>
</html>
<!-- 1. 引入地址文件 -->
<script src="./libs/https.js"></script>

<!-- 导入模态框js文件 -->
<script src="./libs/bootstrap/js/bootstrap.min.js"></script>

<script>
  $(function () {
    // 2. 封装函数，获取用户详情，渲染页面
    function locaData () {
      $.ajax({
        type: 'get',
        url: BigNew.user_detail,
        success: function (backData) {
          // console.log(backData)
          if (backData.code == 200) {
            let data = backData.data

            // 3. 冗余  用循环对象解决
            // $('.username').val(data.username)
            // $('.nickname').val(data.nickname)
            // $('.email').val(data.email)
            // $('.password').val(data.password)
            for (let key in data) {
              // console.log(key)
              // console.log(data[key])
              $('.' + key).val(data[key])
            }
            $('.user_pic').attr('src', data.userPic)
          }
        }
      })
    }

    // 4. 监控上传图片按钮
    $('#exampleInputFile').on('change', function (e) {
      e.preventDefault()
      let picFile = this.files[0]
      let picUrl = URL.createObjectURL(picFile)
      $('.user_pic').attr('src', picUrl)
    })

    // 5. 点击 修改 按钮，获取用户数据，发送ajax请求
    $('.btn-edit').on('click', function (e) {
      e.preventDefault()
      // 5.1 获取数据
      let fd = new FormData(document.querySelector('form'))
      // console.log(fd.get('email'))
      // console.log(fd.get('nickname'))
      // console.log(fd.get('password'))
      // console.log(fd.get('userPic'))
      // console.log(fd.get('username'))

      // 5.2 发送ajax请求
      $.ajax({
        type: 'post',
        url: BigNew.user_edit,
        data: fd,
        // 5.3 解除加密
        contentType: false,
        processData: false,
        success: function (backData) {
          // console.log(backData)
          if (backData.code == 200) {
            $('.modal-body p').text(backData.msg)
            $('#myModal').modal()
            $('#myModal').on('hidden.bs.modal', function () {
              // locaData()
              // window.parent.location = './index.html'
              // 可直接刷新页面
              window.parent.location.reload()
            })
          }
        }
      })
    })

    // 6. 函数自调用
    locaData()
  })
</script>
